<template>
    <view :style="objColor">
        <!-- mine/pages/godaddy/godaddy.wxml -->
        <block v-if="rechargeLists.length > 0">
            <view style="padding-bottom: 150rpx">
                <view class="reRecBox">
                    <view class="record_item" @tap="check" :data-index="index" v-for="(item, index) in rechargeLists" :key="index">
                        <view class="itemDisplay1">
                            <image :src="item.check ? sellerNo=='jw'?'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkbox_sel-blue.png':'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkRefund_sel.png' : 'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkRefund.png'" class="img1"></image>
                            <view class="itemRight">
                                <view class="itemTitle">
                                    <view class="name" :style="item.offerRuleName ? '' : 'margin-top: 25rpx;'">充值时间：{{ item.time }}</view>
                                </view>
                                <view class="changeAmount1" :style="item.offerRuleName ? '' : 'margin-top: -60rpx;'">￥{{ item.chargeMoney }}</view>
                                <view v-if="item.offerRuleName">
                                    <view class="time">{{ item.offerRuleName }}</view>
                                </view>
                            </view>
                        </view>
                    </view>
                    <view class="noList" v-if="LoadingComplete">全部加载完成</view>
                </view>
            </view>
            <view class="fot_btn">
                <view class="fot_btn_left">
					<template v-if="sellerNo != 'hyc' && sellerNo != 'xc'">
                    <image :src="allCheck ? sellerNo=='jw'?'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkbox_sel-blue.png':'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkRefund_sel.png' : 'https://oos-cn.ctyunapi.cn/front-end/static/mine/images/checkRefund.png'" class="img" @tap="checkAll"></image>
                    <view class="txt">本页全选</view>
					</template>
                    <view class="txt1">
                        已选择:
                        <view class="a">{{ allPrice }}元</view>
                    </view>
                </view>
                <view class="btn" @tap="parseEventDynamicCode($event, submitStatus ? 'submit' : '')">提交退款申请</view>
            </view>
        </block>
        <block v-if="rechargeLists.length<=0&&loading">
            <Upgrading text="暂无充值订单"></Upgrading>
        </block>
        <u-popup :round="20" mode="center"  custom-style="width: 85%;padding:0 32rpx;padding-top: 40rpx;box-sizing: border-box;" @close="onCloseEnter" :show="showEnter">
            <view class="tips">退款提醒</view>
            <view class="info">退款后，参与充值活动赠送的优惠券及奖励金将自动失效，确定退款？</view>
            <view class="customerBtn">
                <view class="btn1" @tap="onCloseEnter">取消</view>
                <view class="btn2" @tap="enter">确定退款</view>
            </view>
        </u-popup>
    </view>
</template>

<script>
	'use strict';
	import Upgrading from '../../../components/upgrading/upgrading';

	// exports.__esModule = true; // mine/pages/godaddy/godaddy.ts
	var app = getApp();
	var https_1 = require('./../../../api/https.js');
	export default {
		components: {
			Upgrading
		},
		data() {
			return {
				sellerNo :app.globalData.sellerNo,
				allCheck: false,
				rechargeLists: [
					// { time: '2022-1-1 13:11:11', money: 18.36, tag: '赠送优惠券', check: false },
					// { time: '2022-1-1 13:11:11', money: 18.36, tag: '', check: false },
					// { time: '2022-1-1 13:11:11', money: 18.36, tag: '赠送优惠券', check: false },
				],
				allPrice: 0,
				chargeIds: [],
				page: 1,
				pages: 1,
				pageSize: 10,
				LoadingComplete: false,
				showEnter: false,
				submitStatus: true,
				loading:false
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function () {},
		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function () {},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function () {
			this.getRefundList('replace');
		},
		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function () {
			//调用刷新时将执行的方法
			this.page=1,
				this.pages= 1,
				this.rechargeLists= []
			this.getRefundList('replace');
		},
		//  * 页面上拉触底事件的处理函数
		onReachBottom: function () {
			var that = this;
			var page = that.page + 1;

			if (that.page < that.pages) {
				that.page=page
				this.getRefundList('');
			}
		},
		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function () {
			this.allPrice=0
		},
		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function () {},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function () {},
		methods: {
			//退款列表
			getRefundList: function (type) {
				uni.showLoading({
            title: '加载中...'
        });
				var that = this;
				that.loading=false
				https_1
					.refundList({
						page: this.page,
						pageSize: this.pageSize,
						refundType: 0
					})
					.then(function (res) {
						uni.hideLoading();
						if (res.code == 0) {
							var data = res.data.rechargeLists;
							data.forEach(function (e) {
								e.check = false;
								e.chargeMoney = (e.chargeMoney / 100).toFixed(2);
							});

							that.page= res.data.pageNo,
								that.pages=res.data.pages

							if (res.data.pageNo == res.data.pages) {
								that.LoadingComplete=true
							}

							var list = data || [];

							if (list.length == 0 && res.data.pageNo == 1) {
								that.rechargeLists= []
								that.loading=true
								return false;
							}

							if (type === 'replace') {
								that.rechargeLists= list
							} else {
								that.rechargeLists= that.rechargeLists.concat(list)
							}
							
							that.loading=true
						}
					});
			},

			//选择选择需要退款的充值单
			check: function (event) {
				var that = this;
				var index = event.currentTarget.dataset.index;
				var list = this.rechargeLists;
				var money = 0;
				var chargeIds = [];
				var allPrice = this.allPrice;
				
				// 好易充不多选
				// if(that.sellerNo == 'hyc'){
				// 	list.forEach(item=>{
				// 		item.check = false
				// 	})
				// }
				// 赵忠丽要求全部改成单笔退款，
				list.forEach(item=>{
					item.check = false
				})
				list.forEach(function (e, i) {
					if (i == index) {
						e.check = !e.check;
					}

					if (e.check == true) {
						chargeIds.push(e.id);
						money += parseFloat(e.chargeMoney);
						var money1 = money.toFixed(2);
						that.allPrice= money1,
							that.allCheck= true,
							that.chargeIds= chargeIds
					}
				});
				list.forEach(function (e1) {
					if (e1.check == false) {
						var money1 = money.toFixed(2);
						that.allPrice= money1,
							that.allCheck=false,
							that.chargeIds=chargeIds
					}
				});
				this.rechargeLists= list
			},

			//选择全部
			checkAll: function () {
				var list = this.rechargeLists;
				var money = 0;
				var chargeIds = [];
				this.allCheck=!this.allCheck

				if (this.allCheck) {
					list.forEach(function (element) {
						element.check = true;
						money += parseFloat(element.chargeMoney);
						chargeIds.push(element.id);
					});
					this.chargeIds= chargeIds,
						this.allPrice= money.toFixed(2),
						this.rechargeLists= list
				}

				if (!this.allCheck) {
					list.forEach(function (element) {
						element.check = false;
					});
					this.chargeIds=[],
						this.allPrice=0,
						this.rechargeLists=list
				}
			},

			//提交退款申请
			submit: function () {
				var that = this;
				var list = this.rechargeLists;
				that.submitStatus=false
				console.log("打印提交次数",that.submitStatus,list)
				if (this.allPrice == 0) {
					that.submitStatus=true
					uni.showModal({
						title: '信息提示',
						content: '请选择需要退款的充值单',
						showCancel: false
					});
					return;
				}

				list.forEach(function (element) {
					if (element.check && (element.ruleType == 2 || element.ruleType == 3)) {
						that.showEnter=true
					}
				});

				if (!this.showEnter) {
					this.enter();
				}

			},

			enter: function () {
				uni.showLoading({
					title:"提交中~"
				})
				var that=this
				that.showEnter=false
				https_1
					.refundApply({
						balance: this.allPrice * 100,
						chargeIds: this.chargeIds
					})
					.then(function (res) {
						that.submitStatus=true
						that.page=1
						that.pages=1
						// that.rechargeLists= []
						if (res.code == 0) {
							uni.hideLoading()
							uni.navigateTo({
								url: '../godaddySuccess/godaddySuccess?msg='+'你的退款申请提交成功，我们会尽快处理的，请耐心等待~'
							});
						} else {
							uni.hideLoading()
							uni.showModal({
								title: '信息提示',
								content: res.msg,
								showCancel: false
							});
						}

					});
			},

			onCloseEnter: function () {
				this.showEnter=false
				this.submitStatus=true
			}
		}
	};
</script>
<style lang="less" scoped>
    /* mine/pages/godaddy/godaddy.wxss */
    @import url('./godaddy.less');
</style>
